package ca.swank.hq.hard;

import ca.swank.hq.*;
import ca.swank.hardquor.*;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.util.HashSet;
import java.awt.event.*;

/**
 * modified version of hqSimpleClient window that takes the skin to use as a parameter
 */
public class hqSimpleClientWindow extends JFrame
{
  // the supplied skinfile
  private String skinFile;


  public hqSimpleClientWindow(String skinFile)
  { this.skinFile = skinFile;
    getContentPane().add(new hqSimpleClientPanel(this));
    setSize(550,500);
    setTitle("HardUI Client");
  }

  public static void main(String[] args)
  { String skinFile = null;

    if(args.length >= 1)
    { skinFile = args[0];
    }

    hqSimpleClientWindow window = new hqSimpleClientWindow(skinFile);
    window.show();
  }


class hqSimpleClientPanel extends JPanel implements hardquorUser, ActionListener
{
  /* MOVE TO PROPERTIES / PREFERENCES CLASS */
  private static final String SERVERS_URL = "http://swank.ca/hardquor/servers.properties";

  private hardquorClient gameLogic;

  private JComboBox ipEnter;
  private JButton connectSubmit;

  private JTextField usernameEnter;
  private JButton usernameSubmit;

  private JTextField commentEnter;
  private JButton commentSubmit;
  private hardquorSmartTextArea commentHistory;

  private hardquorArrayListList userList;

  private hardquorPlayerNumberSelect playerNumberSelect;
  private JButton gameRequestSubmit;

  private hardquorArrayListList gameRequestList;
  private JButton gameRequestAcceptSubmit;

  public hqSimpleClientPanel(hqSimpleClientWindow window)
  {
    gameLogic = new hardquorClient(this);

    window.addWindowListener(new hqSimpleClientWindowCloseHandler(gameLogic));

    constructComponents();
    populateGameServerList();
    addComponentsToPanel();
  }

  private void constructComponents()
  {
    ipEnter = hardquorSwingMaker.comboBox(true);
    connectSubmit = hardquorSwingMaker.button("CONNECT",this);

    usernameEnter = hardquorSwingMaker.textField("",20,this);
    usernameSubmit = hardquorSwingMaker.button("SUBMIT USERNAME",this);

    commentEnter = hardquorSwingMaker.textField("", 30, this);
    commentSubmit = hardquorSwingMaker.button("SUBMIT COMMENT",this);
    commentHistory = new hardquorSmartTextArea(40, 10);
    commentHistory.setEnabled(false);


    userList = new hardquorArrayListList();
    userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    playerNumberSelect = new hardquorPlayerNumberSelect();
    gameRequestSubmit = hardquorSwingMaker.button("REQUEST GAME",this);

    gameRequestList = new hardquorArrayListList();

    gameRequestAcceptSubmit = hardquorSwingMaker.button("ACCEPT REQUEST",this);

    setEnabledUsernameControls(false);
    setEnabledUserControls(false);

  }

  private void populateGameServerList()
  { String[][] servers = hardquorInternetTools.getGameServerList(SERVERS_URL);
    for(int i = 0; i < servers.length; i++)
    { ipEnter.addItem(servers[i][1]);
    }

  }

  private void addComponentsToPanel()
  {
	setLayout(new GridBagLayout());

    add(hardquorSwingMaker.label("IP:"),                    hardquorSwingMaker.cell(0,0,"B",1,1));
    add(ipEnter,                                            hardquorSwingMaker.cell(1,0,"B",2,1));
    add(connectSubmit,                                      hardquorSwingMaker.cell(3,0,"B",1,1));

    add(hardquorSwingMaker.label("username:"),              hardquorSwingMaker.cell(0,1,"B",1,1));
    add(usernameEnter,                                      hardquorSwingMaker.cell(1,1,"B",2,1));
    add(usernameSubmit,                                     hardquorSwingMaker.cell(3,1,"B",1,1));

    add(hardquorSwingMaker.label("users online:"),          hardquorSwingMaker.cell(0,2,"B",2,1));
    add(hardquorSwingMaker.label("incoming game requests:"),hardquorSwingMaker.cell(2,2,"B",2,1));

    add(hardquorSwingMaker.scrollPane(userList),            hardquorSwingMaker.cell(0,3,"B",2,1));
    add(hardquorSwingMaker.scrollPane(gameRequestList) ,    hardquorSwingMaker.cell(2,3,"B",2,1));

    add(playerNumberSelect,                                 hardquorSwingMaker.cell(0,4,"B",1,1));
    add(gameRequestSubmit,                                  hardquorSwingMaker.cell(1,4,"B",1,1));
    add(gameRequestAcceptSubmit,                            hardquorSwingMaker.cell(2,4,"B",2,1));

    add(hardquorSwingMaker.scrollPane(commentHistory),      hardquorSwingMaker.cell(0,5,"B",4,1));

    add(commentEnter,                                       hardquorSwingMaker.cell(0,6,"B",3,1));
    add(commentSubmit,                                      hardquorSwingMaker.cell(3,6,"B",1,1));

  }

  public void setEnabledConnectControls(boolean enabled)
  { ipEnter.setEnabled(enabled);
    connectSubmit.setEnabled(enabled);
  }

  public void setEnabledUsernameControls(boolean enabled)
  { usernameEnter.setEnabled(enabled);
    usernameSubmit.setEnabled(enabled);
  }

  public void setEnabledUserControls(boolean enabled)
  {
	userList.setEnabled(enabled);
    playerNumberSelect.setEnabled(enabled);
    gameRequestSubmit.setEnabled(enabled);

    gameRequestList.setEnabled(enabled);
    gameRequestAcceptSubmit.setEnabled(enabled);

    commentEnter.setEnabled(enabled);
    commentSubmit.setEnabled(enabled);
  }


  public void actionPerformed(ActionEvent e)
  {
	if(e.getSource() == usernameSubmit || e.getSource() == usernameEnter)
	{ gameLogic.tryUsername(usernameEnter.getText());

    } else if(e.getSource() == commentSubmit || e.getSource() == commentEnter)
	{ gameLogic.speak(commentEnter.getText());
	  commentEnter.setText("");

    } else if(e.getSource() == gameRequestSubmit)
	{ gameLogic.makeGameRequest(new hardquorGameRequest(playerNumberSelect.getValue(), (String)userList.getSelectedOption()));

    } else if(e.getSource() == gameRequestAcceptSubmit)
	{ hardquorGameRequest current = (hardquorGameRequest)gameRequestList.getSelectedOption();
	  gameRequestList.removeOption(current);
	  gameLogic.acceptGameRequest(current);

    } else if(e.getSource() == connectSubmit)
	{ if(gameLogic.connect(ipEnter.getSelectedItem().toString()))
	  { setEnabledConnectControls(false);
	    setEnabledUsernameControls(true);
	  }
    }


  }

  public void notifyRequestUserName(String reason)
  { System.out.println("username request: " + reason);
  }

  public void notifyUserList(String[] users)
  { userList.addOptions(users);
  }

  public void notifyUserGone(String username)
  { userList.removeOption(username);
  }

  public void notifyUserNew(String username)
  { userList.addOption(username);
    setEnabledUserControls(true);
    setEnabledUsernameControls(false);
  }

  public void notifySpeak(String user, String message)
  { commentHistory.writeln(user + ": " + message);
  }

  public void notifyGameRequest(hardquorGameRequest request)
  { gameRequestList.addOption(request);
  }

  public hardquorUserGame startGame(hardquorGameClient gameDetails, int playerNumber)
  { return new HardUI(gameDetails, playerNumber, skinFile);
  }

}

class hqSimpleClientWindowCloseHandler extends WindowAdapter
{
  private hardquorClient gameLogic;

  public hqSimpleClientWindowCloseHandler(hardquorClient gameLogic)
  { this.gameLogic = gameLogic;
  }

  public void windowClosing(WindowEvent e)
  { gameLogic.disconnect();
    System.exit(0);
  }

}

}

